package potions;

import impsoft.scripting.ibot.itemrec.ItemNameExact;
import impsoft.scripting.ibot.itemrec.ItemRegex;

import java.util.regex.Pattern;

import base.DreamScript;
import base.DreamUtility;

public class PotionUtils extends DreamUtility {
	private int potionFrequency = 0;
	public static final ItemNameExact VIAL = new ItemNameExact("Vial");

	public PotionUtils(DreamScript cs) {
		super(cs);
	}

	public void randomFrequency() {
		potionFrequency = cs.random(4);
	}

	public boolean dropVials() throws InterruptedException {
		cs.getInventoryUtils().doActionOnAll(VIAL, "drop");

		return cs.theTabs.Inventory2.count(VIAL) < 1;
	}

	public boolean needPotion(Potion potion) throws InterruptedException {
		if (potion.getPotionType().equals(PotionType.RESTORATION)) {
			if (potion.equals(Potion.ANTIPOISON)) {
				return cs.theMiniMapIndicators.isPoisioned();
			} else if (potion.equals(Potion.PRAYER)) {
				return cs.getUtils().getPrayerPercentage() < 35;
			}
		} else {
			return cs.theTabs.Statistics.getStatTop(potion.getSkill())
					- cs.theTabs.Statistics.getStatBottom(potion.getSkill()) <= potionFrequency;
		}

		return false;
	}

	public ItemRegex getItemRegex(Potion potion) {
		return new ItemRegex(Pattern.compile(potion.getName() + " \\([1-4]\\)", Pattern.CASE_INSENSITIVE));
	}

	public boolean hasPotion(Potion potion) throws InterruptedException {
		return cs.theTabs.Inventory2.count(getItemRegex(potion)) > 0;
	}

	public boolean usePotion(Potion potion) throws InterruptedException {
		if (hasPotion(potion)) {
			cs.log("Using " + potion.getName());
			return cs.getInventoryUtils().doInventory(getItemRegex(potion), null, true).isSuccessful();
		}

		return false;
	}
}
